export default {
  /**
   * @description token在Cookie中存储的天数，默认1天
   */
  cookieExpires: 1,
  /**
   * @description 是否使用国际化，默认为false
   *              如果不使用，则需要在路由中给需要在菜单中展示的路由设置meta: {title: 'xxx'}
   *              用来在菜单中显示文字
   */
  useI18n: false,
  baseUrl: '/ui',
  // 令牌在cookie中的键名
  tokenCookieKey: 'atcn',
  urlParameterKey: 'access_token',
  redirectUriCookieKey: 'redirect_uri',
  // 认证类型， 本址：local,远程：remote
  authorizeType: 'remote',
  // 本地址登录认证地址
  localLoginUrl: '/login',
  // 远程认证地址
  authorizeUrl: 'http://wuxie.com:8087/oauth/authorize?client_id=6aeea4ac0a114e9f905a2974d97b2e91&response_type=token&redirect_uri=http://wuxie.cn:8088/ui/',
  // 时间转字符串
  dateToString: function (dateValue) {
    if (dateValue instanceof Date) {
      let ardate = [dateValue.getFullYear(), dateValue.getMonth() + 1, dateValue.getDate(), dateValue.getHours(), dateValue.getMinutes(), dateValue.getSeconds()]
      for (let i = 0; i < ardate.length; i++) {
        ardate[i] = ardate[i] < 10 ? '0' + ardate[i].toString() : ardate[i]
      }
      return ardate.slice(0, 3).join('-') + ' ' + ardate.slice(3).join(':')
    } else {
      return ''
    }
  },
  // 修正地址处理
  amendLocationHandler: function (context) {
    const responseToken = context.getParameter(this.urlParameterKey)
    const token = context.getToken()
    if (responseToken && token) {
      let redirectUri = context.getRedirectUri()
      if (!redirectUri || redirectUri === '') {
        redirectUri = '/'
      }
      context.setRedirectUri('')
      location.href = this.baseUrl + '#' + redirectUri
    }
  },
  // 未登录处理
  unLoginHandler: function (context, config, to, from, next) {
    if (this.authorizeType === 'remote') {
      const responseToken = context.getParameter(this.urlParameterKey)
      if (responseToken) {
        context.setToken(responseToken)
        let redirectUri = context.getRedirectUri()
        if (!redirectUri || redirectUri === '') {
          redirectUri = '/'
        }
        context.setRedirectUri('')
        location.href = this.baseUrl + '#' + redirectUri
      } else {
        context.setRedirectUri(to.path)
        location.href = config.authorizeUrl
      }
      next(false)
    } else {
      // context.setRedirectUri(to.path)
      location.href = config.localLoginUrl + '?redirect_uri=' + this.baseUrl + '#' + to.path
    }
  },
  // 未认证处理
  unAuthorized: function (config) {
    if (this.authorizeType === 'remote') {
      location.href = config.authorizeUrl
    } else {
      // context.setRedirectUri(to.path)
      location.href = config.localLoginUrl + '?redirect_uri=' + this.baseUrl
    }
  }
}
